﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;
using Enterprise.Infrastructure;
namespace Enterprise.UI.Web.Manage.WorkFlow
{
    public partial class Manage : System.Web.UI.Page
    {
        int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnCommand();
            }
        }

        private void OnCommand()
        {
            switch (Cmd)
            {
                case "New":
                    HiddenDisp();
                    break;
                case "Edit":
                    HiddenDisp();
                    OnBindData();
                    break;
            }
        }

        private void OnBindData()
        {
            if (Id == 0)
                return;

            WorkFlowInfoService wService = new WorkFlowInfoService();
            var q = wService.WorkFlowDisp(Id);
            if (q != null)
            {
                tb_Name.Text = q.WFNAME;
                tb_Version.Text = q.WFVERSION.ToString();
                tb_Remark.Text = q.WFREMARK;
            }
        }

        private void HiddenDisp()
        {

        }

        private void HiddenInput()
        {
            tb_Name.Visible = false;
            tb_Remark.Visible = false;
            tb_Version.Visible = false;
            SubmitTr.Visible = false;
        }

        protected void Button1_OnClick(object sender, EventArgs e)
        {
            WorkFlowInfoEntity entity = new WorkFlowInfoEntity();
            WorkFlowInfoService Service = new WorkFlowInfoService();
            switch (Cmd)
            {
                case "New":
                    entity.DB_Option_Action = "Insert";
                    break;
                case "Edit":                    
                    entity = Service.WorkFlowDisp(Id);
                    entity.DB_Option_Action = "Update";
                    break;
            }
            entity.WFNAME = (string)Utility.sink(tb_Name.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            entity.WFVERSION = (int)Utility.sink(tb_Version.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            entity.WFTYPE = "";
            entity.WFREMARK = (string)Utility.sink(tb_Remark.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            Service.WorkFlowExecute(entity);
            Response.Redirect("Index.aspx");
            Response.End();
        }
    }
}